package net.toocruel.iqismart.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Calendar;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/6 上午8:12
 */
@Component
public class FileUploadUtils {
    public static String PREFIX_IMAGE_AVATAR = File.separator+"upload"+File.separator+"images"+File.separator+"avatar";
    public static String PREFIX_IMAGE_ATTACHMENT = File.separator+"upload"+File.separator+"images"+File.separator+"attachment";

    @Value("${web.upload.path}")
    private String uploadPath;

    public String upload(InputStream inputStream,String ext) {
        try {
            String path = getAttachement(ext);
            File saveFile = new File(uploadPath+path  );
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            IOUtils.copy(inputStream,new FileOutputStream(saveFile));
            return path;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败:"+e.getLocalizedMessage());
        }
    }

    public String upload(InputStream inputStream,String ext,int width,int height) {
        try {
            String path = getAttachement(ext);
            File saveFile = new File(uploadPath+path  );
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
//            IOUtils.copy(inputStream,new FileOutputStream(saveFile));
            Thumbnails.of(inputStream).size(width,height).outputQuality(1).toFile(saveFile);
            return path;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败:"+e.getLocalizedMessage());
        }
    }

    /**
     * 上传头像
     * @param userId
     * @param inputStream
     * @throws IOException
     */
    public String uploadAvatar(Long userId,InputStream inputStream) {
        // /upload/image/avatar/11/2/1.png
        try {
            File avatarFile = new File(uploadPath+getAvatar(userId)  );
            if (!avatarFile.getParentFile().exists()) {
                avatarFile.getParentFile().mkdirs();
            }

            File avatarFullFile = new File(uploadPath+getFullAvatar(userId)  );
            if (!avatarFullFile.getParentFile().exists()) {
                avatarFullFile.getParentFile().mkdirs();
            }

//            IOUtils.copy(inputStream,new FileOutputStream(avatarFile));
//            IOUtils.copy(new FileInputStream(avatarFile),new FileOutputStream(avatarFullFile));
            try {
                FileOutputStream outputStream = new FileOutputStream(avatarFile);
                byte[] bytes = new byte[1024];
                while(inputStream.read(bytes)>0){
                    outputStream.write(bytes);
                }
                outputStream.close();

                outputStream = new FileOutputStream(avatarFullFile);
                FileInputStream fileInputStream = new FileInputStream(avatarFile);
                bytes = new byte[1024];
                while(fileInputStream.read(bytes)>0){
                    outputStream.write(bytes);
                }
                outputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                inputStream.close();

            }


            return getAvatar(userId);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败:"+e.getLocalizedMessage());
        }
    }

    public static String getAvatar(Long userId){
        return PREFIX_IMAGE_AVATAR+File.separator+StringUtils.join(MyStringUtils.getStrList(10000+userId + "", 2),File.separator)+".png";
    }


    public static String getFullAvatar(Long userId){
        return  PREFIX_IMAGE_AVATAR+File.separator+StringUtils.join(MyStringUtils.getStrList(10000+userId + "", 2),File.separator)+"_full.png";
    }



    public String getAttachement(String ext) {
       return PREFIX_IMAGE_ATTACHMENT+File.separator + Calendar.getInstance().get(Calendar.YEAR)+ File.separator + Calendar.getInstance().get(Calendar.MONTH)+ File.separator+ Calendar.getInstance().get(Calendar.DAY_OF_MONTH) +File.separator+System.currentTimeMillis()%(1000L*60*60*24)+"."+ext;
    }

    /**
     * 相对站长的路径 转换为绝对路径
     * @param path
     * @return
     */
    public String getRealPath(String path){
        return uploadPath+File.separator+path;
    }





}
